import time
import datetime
from selenium import webdriver
from movieSpider import movieSpider

#设置定时
def timing(h,m):
    while True:
        now = datetime.datetime.now()
        print('当前时间为:'+str(now.hour)+':'+str(now.minute))
        if(now.hour == h and now.minute == m):
            print('准备开始任务...')
            startSpider()
        time.sleep(60)

#爬虫任务，爬昨天的数据
def startSpider():
    today = datetime.datetime.today()
    yesterday = today - datetime.timedelta(days=1)
    yesterday_str = yesterday.strftime("%Y-%m-%d")

    print('**** 开始爬取'+yesterday_str+'的电影数据，数据来源：猫眼电影 ***')

    ##### 初始化Spider（连接数据库，连接百度Ocr）
    mySpider = movieSpider()
    ##### 开始
    options = webdriver.ChromeOptions()
    options.add_argument('blink-settings=imagesEnabled=false')
    options.add_argument('headless')
    driver = webdriver.Chrome(executable_path='/Users/aaa/py/chromedriver',options=options)
    mySpider.getMovieDataByDate(driver,yesterday_str)
    driver.quit()
    

#定时中午12时，爬昨日数据
timing(12,00)






